//
//  ZGWHuDongViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/6/13.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "ZGWHuDongViewController.h"
#import "YiJiYinChatListViewController.h"
#import "ChatRoomViewController.h"
#import "SelectActivityViewController.h"
#import "MyFriendViewController.h"
#import "MyGroupViewController.h"
#import <RongIMKit/RongIMKit.h>

#define kCurrentUserToken [[NSUserDefaults standardUserDefaults]objectForKey:@"token"]

@interface ZGWHuDongViewController ()

@property (nonatomic, strong) UIImageView *topImage; // 顶部图片
@property (nonatomic, strong) UIImageView *messageImage; // 消息图片
@property (nonatomic, strong) UIImageView *friendImage; // 我的好友图片
@property (nonatomic, strong) UIImageView *chatRoomImage; // 聊天室图片
@property (nonatomic, strong) UIImageView *activityImage; // 互动图片
@property (nonatomic, strong) UIImageView *groupImage; // 我的小组图片

@property (nonatomic, strong) UIImageView *navBarHairlineImageView; // 黑色条

@end

@implementation ZGWHuDongViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    self.navBarHairlineImageView.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navBarHairlineImageView.hidden = NO;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //   self.navigationController.navigationBar.titleTextAttributes = @{UITextAttributeTextColor:[UIColor whiteColor], UITextAttributeFont:[UIFont boldSystemFontOfSize:18]};
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 更改Navi
    [self changeNavi];
    // 布局
    [self addSubview];
}

// 更改Navi
- (void)changeNavi
{
    // iOS7.0以后需要这样设置
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:22]};
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1]];
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.title = @"互动";
    // [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1]];
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:tapGes];
    // 取消黑条
    
    [self hideTheBlackLine];
}

- (void)hideTheBlackLine
{
    self.navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

#pragma mark - 返回上一页
- (void)lastPage:(UIBarButtonItem *)barItem
{
    [self.navigationController popViewControllerAnimated:YES];
}

// 布局
- (void)addSubview
{
    self.topImage = [[UIImageView alloc] initWithFrame:CGRectMake(-1 * kAdjustParamater, -1 * kAdjustParamater, kScreenWidth + 2 * kAdjustParamater, 200 * kAdjustParamater)];
    self.topImage.image = [[UIImage imageNamed:@"dingbu-hudong"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.view addSubview:_topImage];
    
    // 消息
    self.messageImage = [[UIImageView alloc] initWithFrame:CGRectMake(1 * kAdjustParamater,self.topImage.bottom + 10 * kAdjustParamater, 124 * kAdjustParamater, 124 *kAdjustParamater)];
    self.messageImage.image = [UIImage imageNamed:@"message-hudong"];
    self.messageImage.userInteractionEnabled = YES;
    self.messageImage.tag = 100;
    UITapGestureRecognizer *tapGes1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage:)];
    [self.messageImage addGestureRecognizer:tapGes1];
    [self.view addSubview:_messageImage];
    
    // 我的好友
    self.friendImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.messageImage.right + 1 * kAdjustParamater, self.messageImage.top, 124 * kAdjustParamater, 124 * kAdjustParamater)];
    self.friendImage.image = [UIImage imageNamed:@"friend-hudong"];
    self.friendImage.userInteractionEnabled = YES;
    self.friendImage.tag = 101;
    UITapGestureRecognizer *tapGes2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage:)];
    [self.friendImage addGestureRecognizer:tapGes2];
    [self.view addSubview:_friendImage];
    
    // 聊天室
    self.chatRoomImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.friendImage.right + 1 * kAdjustParamater, self.messageImage.top, 123 * kAdjustParamater, 124 * kAdjustParamater)];
    self.chatRoomImage.image = [UIImage imageNamed:@"charroom-hudong"];
    self.chatRoomImage.userInteractionEnabled = YES;
    self.chatRoomImage.tag = 102;
    UITapGestureRecognizer *tapGes3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage:)];
    [self.chatRoomImage addGestureRecognizer:tapGes3];
    [self.view addSubview:_chatRoomImage];
    
    
//    // 我的小组
//    self.groupImage = [[UIImageView alloc] initWithFrame:CGRectMake(1 * kAdjustParamater, self.messageImage.bottom + 1 * kAdjustParamater, (375 - 3) * kAdjustParamater / 2, 124 * kAdjustParamater)];
//    self.groupImage.image = [UIImage imageNamed:@"group-hudong"];
//    self.groupImage.userInteractionEnabled = YES;
//    self.groupImage.tag = 103;
//    UITapGestureRecognizer *tapGes4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage:)];
//    [self.groupImage addGestureRecognizer:tapGes4];
//    [self.view addSubview:_groupImage];
//    
//    // 活动
//    self.activityImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.groupImage.right + 1 * kAdjustParamater, self.messageImage.bottom + 1 * kAdjustParamater, (375 - 3) * kAdjustParamater / 2, 124 * kAdjustParamater)];
//    self.activityImage.image = [UIImage imageNamed:@"activity-hudong"];
//    self.activityImage.userInteractionEnabled = YES;
//    self.activityImage.tag = 104;
//    UITapGestureRecognizer *tapGes5 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage:)];
//    [self.activityImage addGestureRecognizer:tapGes5];
//    [self.view addSubview:_activityImage];
    
    UILabel *bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.messageImage.bottom + 10 * kAdjustParamater, kScreenWidth, 400 * kAdjustParamater)];
    //  bottomLabel.backgroundColor = [UIColor colorWithRed:122 / 255.0 green:194 / 255.0 blue:221 / 255.0 alpha:1];
    bottomLabel.backgroundColor = kCyanColor;
    [self.view addSubview:bottomLabel];
    
    
}

#pragma mark - 下一页
- (void)nextPage:(UITapGestureRecognizer *)tapGes
{
    // 判断用户是否登录
    if ([NSString stringWithFormat:@"%@", kCurrentUserToken].length == 6 || [kCurrentUserToken isEqualToString:@"(null)"]) {
        // 说明用户尚未登录
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"本界面的功能需要登录后才能使用，您确定要现在登录么？" preferredStyle:UIAlertControllerStyleAlert];
        // 修改标题颜色
        NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:@"温馨提示"];
        [titleString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 4)];
        [titleString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 4)];
        [alertVC setValue:titleString forKey:@"attributedTitle"];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
            [delegate showMainViewController:0];
        }];
        [alertVC addAction:sureAction];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertVC addAction:cancelAction];
        [self presentViewController:alertVC animated:YES completion:nil];
    } else {
        // 说明用户登录成功
        // 取出融云的登录状态
        NSString *loginRCloudIsSuccess = [[NetStatusHandle shareHandle] getLoginRCloudIsSuccess];
        if ([loginRCloudIsSuccess isEqualToString:@"YES"]) {
            // 登录成功
            switch (tapGes.view.tag) {
                case 100:
                {
                    YiJiYinChatListViewController *yiJiYinVC = [[YiJiYinChatListViewController alloc] init];
                    [self.navigationController pushViewController:yiJiYinVC animated:yiJiYinVC];
                    NSLog(@"消息");
                    break;
                }
                case 101:
                {
                    NSLog(@"我的好友");
                    MyFriendViewController *myFriendVC = [[MyFriendViewController alloc] init];
                    [self.navigationController pushViewController:myFriendVC animated:YES];
                    break;
                }
                case 102:
                {
                    NSLog(@"聊天");
                    ChatRoomViewController *chatRoomVC = [[ChatRoomViewController alloc] init];
                    [self.navigationController pushViewController:chatRoomVC animated:YES];
                    break;
                }
                case 103:
                {
                    NSLog(@"我的小组");
                    MyGroupViewController *myGroupVC = [[MyGroupViewController alloc] init];
                    [self.navigationController pushViewController:myGroupVC animated:YES];
                    break;
                }
                case 104:
                {
                    NSLog(@"活动");
                    SelectActivityViewController *selectAcitivityVC = [[SelectActivityViewController alloc] init];
                    [self.navigationController pushViewController:selectAcitivityVC animated:YES];
                    break;
                }
                default:
                    break;
            }
        } else {
            // 登录失败，需要重新登录
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的聊天服务器已经断开，您确定要重新登录么？" preferredStyle:UIAlertControllerStyleAlert];
            // 修改标题颜色
            NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"提示"];
            [title addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, title.length)];
            [title addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, title.length)];
            [alertVC setValue:title forKey:@"attributedTitle"];
            UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] startAnimation];
                // 登录融云
                
                NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
                NSString *rong_token=[user objectForKey:@"rong_token"];
                [[RCIM sharedRCIM] connectWithToken:rong_token success:^(NSString *userId) {
                    NSLog(@"重新登录成功");
                   
                    // 保存当前登陆融云的用户ID
                    [[NetStatusHandle shareHandle] saveCurrentLoginRongCloudUserID:userId];
                    // 融云登录成功
                    [[NetStatusHandle shareHandle] saveLoginRCloudIsSuccess:@"YES"];
                    dispatch_async(dispatch_get_main_queue(), ^{
                         [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
                         [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertWithAnimationByTitle:@"登录成功" toView:self];
                    });
                  
                } error:^(RCConnectErrorCode status) {
                     dispatch_async(dispatch_get_main_queue(), ^{
                         [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
                         [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertNoTitieWithContent:@"登录失败，请您退出重新登录" toView:self];
                     });
                    
                } tokenIncorrect:^{
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
                        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertNoTitieWithContent:@"登录失败，请您退出重新登录" toView:self];
                    });
                }];
            }];
            [alertVC addAction:sureAction];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alertVC addAction:cancelAction];
            [self presentViewController:alertVC animated:YES completion:nil];
        }

    }
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
